Location_();
var user_name = getCookie("user_name");
$('.user_name').text(user_name);

var departent_name;
var role_id;
//网谈table
layui.use(['jquery', 'layer', 'table', 'form', 'element'], function() {
	var table = layui.table;
	var element = layui.element;
	var form = layui.form;
	var $ = layui.$ //重点处
		,
		layer = layui.layer;
	//表格渲染
	table.render({
		elem: '#LAY_table_deparment',
		size: 'sm',
		url: "http://"+com_url+"//admin/getResourceList",
		response: {
			statusCode: 200,
			dataName: 'data.result'
		},
		cols: [
			[{
				fixed: 'left',
				checkbox: true,
			}, {
				field: 'unit',
				width: '12%',
				title: '部门',
			}, {
				field: 'group_name',
				width: '12%',
				title: '咨询项目'
			}, {
				field: 'change',
				width: '12%',
				title: '转化项目'
			}, {
				field: 'category',
				width: '12%',
				title: '项目类别'
			}, {
				field: 'resourcept',
				width: '15%',
				title: '推广类别'
			}, {
				field: 'allocation_number',
				width: '12%',
				title: '所需资源',
			}, {
				field: 'right',
				width: '20%',
				toolbar: '#barDemo1',
				title: '操作'
			}]
		],
		page: true,
		limit: 10,
		id: 'adduser_table',
		done: function(res, count) { //回调函数
			var code = res.code;
				if(code==204){
					clearCookie();
					window.parent.top.location.reload();
				}
		}
	});

	active = {
		//搜索功能
		reload_sbm: function() {
			var index = layer.load(1, {
				shade: [0.5, '#fff'] //0.1透明度的白色背景
			});
			var unit = $('#unit').val();
			var perject = $('#project').val();
			//执行重载
			table.reload('adduser_table', {
				page: {
					curr: 1 //重新从第 1 页开始
				},
				where: {
					unit: unit,
					group_name: perject
				},
				done: function(res) {
					layer.close(index);
				}
			});
		},
		//新建部门线索
		add_department_btn: function() {
			layer.open({
				type: 1,
				area: '400px',
				skin: 'add_department_content',
				title: "新建部门线索",
				content: $('#add_department_clue'),
				btn: ["保存", "取消"],
				btn1: function(index, curr) {
					var department_input = $('#department_input').val();
					var project_input = $('#project_input').val();
					var percent_project = $('#percent_project').val();
					var perject_type = $('#perject_type').val();
					var promote_type = $('#promote_type').val();
					var need_resouce = $('#need_resouce').val();
					var add_ROI = $('#add_ROI').val();
					if(department_input && project_input && perject_type && promote_type && need_resouce) {
						$.ajax({
							type: "post",
							beforeSend: cookie_val_fun,
							data: {
								allocation_number: need_resouce,
								group_name: project_input,
								unit: department_input,
								category: perject_type,
								resourcept: promote_type,
								change: percent_project,
								ROI:add_ROI
							},
							url: "http://"+com_url+"//admin/resourceAdd",
							success: function(res) {
								var data = JSON.parse(res);
								var department_list = data.data;
								var code = data.code;
								if(code == "200") {
									layer.close(index)
									$(".layui-laypage-btn")[0].click();
									layer.msg("添加成功", {
										icon: 1
									})
								} else if(code=="207"){
									layer.msg("部门已存在", {
										icon: 0
									})
								}else if(code=="201"){
									layer.msg("数据异常", {
										icon: 0
									})
								}
							},
						});
					} else {
						layer.msg("请认真填写，所有选项都不能为空", {
							icon: 0
						})
					};
				},
				btn2: function(index, curr) {
					$('.add_department_content input,textarea').val("");
				},
				cancel: function(index, curr) {
					$('.add_department_content input,textarea').val("");
				}
			})
		}
	};
	
	$('.keydown_search').on('click', function() {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});

	$('#add_department_btn').on('click', function() {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});

	//监听单元格事件
	var user_names;
	table.on('tool(deparment_leaders)', function(obj) {
		var data = obj.data;
		var datas = JSON.parse(JSON.stringify(data))
		var edit_id = obj.data.id;
		//编辑用户信息
		if(obj.event === 'edit') {
			var unit = obj.data.unit; //部门
			var group_name = obj.data.group_name; //咨询项目
			var category = obj.data.category; //项目类别
			var change_s = obj.data.change;
			var resourcept = obj.data.resourcept; //推广类别
			var allocation_number = obj.data.allocation_number; //所需资源
			var roi = obj.data.ROI;
			
			$('#edit_department_input').val(unit);
			$('#edit_project_input').val(group_name);
			$('#edit_perject_type').val(category);
			$('#edit_promote_type').val(resourcept);
			$('#edit_need_resouce').val(allocation_number);
			$('#edit_percent_project').val(change_s);
			$('#edit_ROI').val(roi);
			layer.open({
				type: 1,
				title: "编辑信息",
				skin: 'add_department_content',
				area: "400px",
				content: $("#Edit_content"),
				btnAlign: 'c',
				btn: ['保存', '取消'],
				btn1: function(index, curr) {
					var edit_need_resouce = $('#edit_need_resouce').val();
					var edit_department_input = $('#edit_department_input').val();
					var edit_project_input = $('#edit_project_input').val();
					var edit_percent_project = $('#edit_percent_project').val();
					var edit_perject_type = $('#edit_perject_type').val();
					var edit_promote_type = $('#edit_promote_type').val();
					var roival = $('#edit_ROI').val();
					$.ajax({
						type: "post",
						beforeSend: cookie_val_fun,
						data: {
							id: edit_id,
							unit: edit_department_input,
							group_name: edit_project_input,
							category: edit_perject_type,
							resourcept: edit_promote_type,
							allocation_number: edit_need_resouce,
							change: edit_percent_project,
							ROI:roival
						},
						url: "http://"+com_url+"//admin/resourceEdit",
						success: function(res) {
							var data = JSON.parse(res);
							var department_list = data.data;
							var code = data.code;
							let msg = data.msg;
							if(code == "200") {
								layer.close(index)
								$(".layui-laypage-btn")[0].click();
								layer.msg(msg, {
									icon: 1
								})
							} else {
								layer.msg(msg, {
									icon: 0
								})
							}
						},
					});
				}
			})
		}else if(obj.event === "area") { //添加区域
			layer.open({
				type: 1,
				title: "添加区域",
				skin: 'add_department_content',
				area: "400px",
				content: $("#add_area"),
				btnAlign: 'c',
				btn: ['保存', '取消'],
				btn1: function(index, curr) {
					var add_companyName = $('#add_companyName').val();
					var add_areas = $('#add_areas').val();
					var add_area_textarea = $('#add_area_textarea').val();
					if(add_companyName && add_areas && add_area_textarea) {
						$.ajax({
							type: "post",
							beforeSend: cookie_val_fun,
							data: {
								aid: edit_id,
								company: add_companyName,
								prime_area: add_areas,
								area: add_area_textarea,
							},
							url: "http://"+com_url+"//admin/areaAdd",
							success: function(res) {
								var data = JSON.parse(res);
								var department_list = data.data;
								var code = data.code;
								let msg = data.msg;
								if(code == "200") {
									$('.add_department_content input,textarea').val("");
									layer.close(index)
									$(".layui-laypage-btn")[0].click();
									$('#revision_input,#revision_text').val("");
									
									layer.msg(msg, {
										icon: 1
									})
								} else {
									layer.msg(msg, {
										icon: 0
									})
								}
							},
						});
					} else {
						layer.msg("请认真填写，所有选项都不能为空", {
							icon: 0
						})
					}
				},
				btn2: function(index, curr) {
					$('.add_department_content input,textarea').val("");
				},
				cancel: function(index, curr) {
					$('.add_department_content input,textarea').val("");
				}
			})
		} else if(obj.event === "revision") { //修改区域
			$.ajax({
				type: "get",
				beforeSend: cookie_val_fun,
				data: {
					aid: edit_id,
				},
				url: "http://"+com_url+"//admin/getArea",
				success: function(res) {
					var data = JSON.parse(res);
					var datas = data.data;
					var code = data.code;
					var msgs = data.msg;
					if(code == "200") {
						layer.open({
							type: 1,
							title: "修改区域",
							skin: 'add_department_content',
							area: "400px",
							content: $("#revision_area"),
							btnAlign: 'c',
							btn: ['保存', '取消','删除'],
							btn1: function(index, curr) {
								$.ajax({
									type: "post",
									beforeSend: cookie_val_fun,
									data: {
										aid: edit_id,
										company:$('#revision_company option:selected').text(),
										id:$('#revision_company option:selected').val(),
										prime_area:$('#revision_input').val(),
										area:$('#revision_text').val()
									},
									url: "http://"+com_url+"//admin/areaEdit",
									success: function(res) {
										var data = JSON.parse(res);
										var datas = data.data;
										var code = data.code;
										var msg = data.msg;
										if(code == "200") {
											$('#revision_input,#revision_text').val("");
											layer.close(index)
											$(".layui-laypage-btn")[0].click();
											
											layer.msg(msg, {
												icon: 1
											})
										} else {
											layer.msg(msg, {
												icon: 0
											})
										}
									}
								})
							},
							btn2:function(index,curr){
								$('#revision_text').val("");
								$('#revision_input').val("");
							},
							cancel:function(index,curr){
								$('#revision_text').val("");
								$('#revision_input').val("");
							},
							btn3: function(index, curr) {
								$.ajax({
									type: "post",
									beforeSend: cookie_val_fun,
									data: {
										id:$('#revision_company option:selected').val(),
									},
									url: "http://"+com_url+"//admin/areaDel",
									success: function(res) {
										var data = JSON.parse(res);
										var datas = data.data;
										var code = data.code;
										var msg = data.msg;
										if(code == "200") {
											form.render('select')
											$('#revision_text').val("");
											$('#revision_input').val("");
											layer.msg(msg, {
												icon: 1
											})
										} else {
											layer.msg(msg, {
												icon: 0
											})
										}
									}
								})
							}
						})
						var revision_company = $('#revision_company');
						revision_company.find('option').remove().end()
						revision_company.append($("<option></option>").attr("value", '').text('--请选择--'));
						$.each(datas, function(index, curr) {
							revision_company.append($("<option></option>").attr("value", curr.id).text(curr.company));
						});
						$('#revision_company').change(function() {
							var ceshis = $(this).val();
							var revision_index = $(this).find('option:selected').index()
							var num = revision_index + 1;
							if(revision_index == "0") {
								$('#revision_input').val("");
								$('#revision_text').val("")
							} else {
								$('#revision_input').val(datas[revision_index - 1].prime_area);
								$('#revision_text').val(datas[revision_index - 1].area)
							}
						})
					} else {
						layer.msg(msgs, {
							icon: 0
						})
					}
				},
			});
		}
	});
});

//文件上传
layui.use('upload', function() {
	var $ = layui.jquery,
		upload = layui.upload;
	//选完文件后不自动上传
	upload.render({
		method: 'post',
		elem: '#test8',
		response: {
			statusCode: 200
		},
		headers: {
			ApiAuth: cookie_val
		},
		url: "http://"+com_url+"//admin/dataExcel",
		exts: 'xls|XLSX',
		auto: false,
		accept: 'file',
		size: 20000,
		bindAction: '#test9',
		done: function(res) {
		},
		error: function(res) {
			alert("数据报错")
		},
	});
});